package com.lcq.floatview;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class FloatMenu {
    private final DragView menuIv;
    private final FrameLayout floatView;
    private final FrameLayout.LayoutParams layoutParams;
    private final Activity activity;
    private boolean addedContentView = false;

    public FloatMenu(final Activity activity) {
        this.activity = activity;
        floatView = (FrameLayout) LayoutInflater.from(activity).inflate(R.layout.floating_menu, null);
        menuIv = floatView.findViewById(R.id.floating_menu_div);
        menuIv.setOnClickCallback(new DragView.OnClickCallback() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "您点击了球球", Toast.LENGTH_LONG).show();
            }
        });
        layoutParams = new FrameLayout.LayoutParams(activity.getResources().getDisplayMetrics().widthPixels,
                activity.getResources().getDisplayMetrics().heightPixels);
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    }

    public void show() {
        if (!addedContentView) {
            activity.getWindow().addContentView(floatView, layoutParams);
            addedContentView = true;
        }
        floatView.setVisibility(View.VISIBLE);
    }

    public void dismiss() {
        floatView.setVisibility(View.INVISIBLE);
    }
}
